package com.example.shopapp.fragment;

import android.content.Intent;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.shopapp.App;
import com.example.shopapp.R;
import com.example.shopapp.adapter.CirCleAdapter;
import com.example.shopapp.adapter.OnCircleListener;
import com.example.shopapp.bean.Guanzhu;
import com.example.shopapp.bean.Luntan;
import com.example.shopapp.dao.GuanzhuDao;
import com.example.shopapp.dao.LuntanDao;
import com.example.shopapp.database.GreenDaoManager;
import com.example.shopapp.databinding.FramentCircleBinding;
import com.example.shopapp.ui.user.AddCircleActivity;
import com.example.shopapp.ui.user.PinglunActivity;
import com.example.shopapp.widget.MyItemDecoration;
import com.kongzue.dialogx.dialogs.MessageDialog;

import java.util.Collections;
import java.util.List;

import cn.project.base.baseui.BaseNormalFragment;

public class CircleFragment extends BaseNormalFragment<FramentCircleBinding> implements OnCircleListener {

    CirCleAdapter cirCleAdapter;
    LuntanDao luntanDao;
    GuanzhuDao guanzhuDao;
    @Override
    protected int getLayoutId() {
        return R.layout.frament_circle;
    }

    @Override
    protected void initData() {
        guanzhuDao = GreenDaoManager.getmInstance().getDaoSession().getGuanzhuDao();
        luntanDao = GreenDaoManager.getmInstance().getDaoSession().getLuntanDao();
        cirCleAdapter = new CirCleAdapter(getActivity(),this);
        mBinding.rlCircle.setLayoutManager(new LinearLayoutManager(getActivity()));
        mBinding.rlCircle.addItemDecoration(new MyItemDecoration(10));
        mBinding.ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), AddCircleActivity.class));
            }
        });

        cirCleAdapter.setNewData(getLuntanList());
        mBinding.rlCircle.setAdapter(cirCleAdapter);
        cirCleAdapter.notifyDataSetChanged();
    }

    @Override
    public void onResume() {
        super.onResume();
        cirCleAdapter.setNewData(getLuntanList());
        mBinding.rlCircle.setAdapter(cirCleAdapter);
        cirCleAdapter.notifyDataSetChanged();
    }

    private List<Luntan> getLuntanList(){
        List<Luntan> allList = luntanDao.loadAll();
        Collections.reverse(allList);
        return allList;
    }

    @Override
    public void onCommentClick(Luntan luntan) {
        Intent intent = new Intent(getActivity(), PinglunActivity.class);
        intent.putExtra("detail", luntan);
        startActivity(intent);
    }

    @Override
    public void onZanClick(String id) {
        List<Guanzhu> list = guanzhuDao.loadAll();
        if (list == null || list.size() ==0){
            Guanzhu guanzhu = new Guanzhu();
            guanzhu.setGuanzhu_id(id);
            guanzhu.setId(System.currentTimeMillis());
            guanzhu.setUser_id(App.getInstance().user.getUser_id());
            guanzhuDao.insert(guanzhu);
            MessageDialog.build()
                    .setTitle("提示")
                    .setMessage("关注成功")
                    .setOkButton("确定")
                    .show();
            cirCleAdapter.setNewData(getLuntanList());
            mBinding.rlCircle.setAdapter(cirCleAdapter);
            cirCleAdapter.notifyDataSetChanged();
            return;
        }
        boolean has = false;
        Guanzhu guanzhu1 = null;
        for (int i = 0; i < list.size(); i++) {
            if (id.equals(list.get(i).getGuanzhu_id())
            && App.getInstance().user.getUser_id().equals(list.get(i).getUser_id())){
                has = true;
                guanzhu1 = list.get(i);
            }
        }
        if (has){
            MessageDialog.build()
                    .setTitle("提示")
                    .setMessage("取消关注")
                    .setOkButton("确定")
                    .show();
            guanzhuDao.deleteByKeyInTx(guanzhu1.getId());
            cirCleAdapter.setNewData(getLuntanList());
            mBinding.rlCircle.setAdapter(cirCleAdapter);
            cirCleAdapter.notifyDataSetChanged();
            return;
        }
        Guanzhu guanzhu = new Guanzhu();
        guanzhu.setGuanzhu_id(id);
        guanzhu.setId(System.currentTimeMillis());
        guanzhu.setUser_id(App.getInstance().user.getUser_id());
        guanzhuDao.insert(guanzhu);
        MessageDialog.build()
                .setTitle("提示")
                .setMessage("关注成功")
                .setOkButton("确定")
                .show();
        cirCleAdapter.setNewData(getLuntanList());
        mBinding.rlCircle.setAdapter(cirCleAdapter);
        cirCleAdapter.notifyDataSetChanged();
    }

    @Override
    public void onSelect(long id) {

    }
}
